<?php
/*
 * @Package Softpin Order Manager
 * @Subpackage : simorder Manager
 * Create by : thuc.lehuy@gmail.com
 */
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
//import view parent class
jimport( 'joomla.application.component.view' );

class SimordersViewSimorders extends JView
{
    function display($tpl = null)
    {
        JToolBarHelper::title(JText::_( 'Quản lý đặt Sim' ), 'generic.png' );
        JToolBarHelper::deleteList();
        JToolBarHelper::publish();
        JToolBarHelper::unpublish();
        JToolBarHelper::help( 'screen.banners' );
        // pager
        global $mainframe, $option;
        $db =& JFactory::getDBO();
        $client = JRequest::getWord( 'filter_client', 'site' );

        $filter_order		= $mainframe->getUserStateFromRequest( "$option.$client.filter_order",'filter_order','p.folder','cmd' );
        $filter_order_Dir	= $mainframe->getUserStateFromRequest( "$option.$client.filter_order_Dir",'filter_order_Dir','','word' );
        $filter_state = $mainframe->getUserStateFromRequest("$option.$client.filter_state", 'filter_state', '', 'word');
        $search                 = $mainframe->getUserStateFromRequest( "$option.$client.search",'search','','string' );
        if (strpos($search, '"') !== false) {
            $search = str_replace(array('=', '<'), '', $search);
        }
        $search = JString::strtolower($search);
        // limit of pager
        $limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
        $limitstart	= $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );
        // init where clause
        $where = '';
        
        if ( $search ) {
            $where[] = 'LOWER( p.email ) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
        }
        if ($filter_state) {
            if ($filter_state == 'P') {
                $where[] = 'p.del_flag = 0';
            } else if ($filter_state == 'U') {
                $where[] = 'p.del_flag = 1';
            }
        }
        $where 	= $where ? ' WHERE ' . implode( ' AND ', $where ) : '';
        // init order clause
        if (!in_array($filter_order, array('p.name', 'p.email', 'p.tel', 'p.require', 'p.datetime_created', 'p.del_flag','p.address','p.sim_numb', 'p.id'))) {
                $filter_order = 'p.sim_order_id';
        }

        if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC'))) {
                $filter_order_Dir = 'DESC';
        }
        $orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir.', p.datetime_created';
        // get the total number of records
        $query = 'SELECT COUNT(*)'
                . ' FROM #__plugins AS p'
                . $where
                ;
        $db->setQuery( $query );
        $total = $db->loadResult();
        //init pagination
        jimport('joomla.html.pagination');
        $pagination = new JPagination( $total, $limitstart, $limit );
        //get data
        $query = 'SELECT p.*'
                . ' FROM #__sim_order AS p'
                . $where
                . ' GROUP BY p.sim_order_id'
                . $orderby
                ;
        
        $db->setQuery( $query, $pagination->limitstart, $pagination->limit );
        $rows = $db->loadObjectList();
        if ($db->getErrorNum()) {
                echo $db->stderr();
                return false;
        }
        // state filter
        $lists['state'] = JHTML::_('grid.state', $filter_state);
        // table ordering
        $lists['order_Dir']	= $filter_order_Dir;
        $lists['order']		= $filter_order;

        // search filter
        $lists['search']= $search;

        $this->assign('client',$client);

        $this->assignRef('user',JFactory::getUser());
        $this->assignRef('lists',$lists);
        $this->assignRef('items',$rows);
        $this->assignRef('pagination',$pagination);

        parent::display($tpl);
    }
    /*
     * Authentication
     * Manager can not edit,new,publish,unpublish
     */
    function authen()
    {
        $user = JFactory::getUser();
        if($user->get('gid') < 24){
            return 0;
        }else{
            return 1;
        }
    }
}